package com.jbp.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jbp.dao.RankProportionDao;
import com.jbp.model.PrizePoolAmount;
import com.jbp.model.PrizePoolEntity;
import com.jbp.model.RankProportion;
import com.jbp.model.SkillUserParam;
import com.jbp.utils.PageUtils;
import com.jbp.utils.Query;
import com.jbp.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class RankProportionService extends ServiceImpl<RankProportionDao, RankProportion> implements IService<RankProportion> {

    @Autowired
    private PrizePoolAmountService prizePoolAmountService;

    @Autowired
    private WalletRecordService walletRecordService;

    @Autowired
    private PropertyService propertyService;

    @Autowired
    private LotteryRecordService lotteryRecordService;

    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<RankProportion> queryWrapper = new QueryWrapper<>();
        if (params.get("id") != null) {
            queryWrapper.like("id",params.get("id"));
        }
        if (params.get("ranks") != null) {
            queryWrapper.like("ranks",params.get("ranks"));
        }
        if (params.get("proportion") != null) {
            queryWrapper.like("proportion",params.get("proportion"));
        }
        if (params.get("createTime") != null) {
            queryWrapper.like("create_time",params.get("createTime"));
        }
        if (params.get("updateTime") != null) {
            queryWrapper.like("update_time",params.get("updateTime"));
        }
        if (params.get("fromDate")!=null) {
            queryWrapper.ge("create_time", params.get("fromDate"));
        }
        if (params.get("toDate")!=null) {
            queryWrapper.le("DATE_FORMAT(create_time,   '%Y-%m-%d')", params.get("toDate"));
        }
        queryWrapper.orderByDesc("id");
        IPage<RankProportion> page = this.page(
            new Query<RankProportion>().getPage(params), queryWrapper
        );
        return new PageUtils(page);
    }


    /**
     * 抽取消耗流水的千分之一，每隔7天一周期
     * @param amount
     */
    public void addPrizePoolAmount(BigDecimal amount) {

        PrizePoolAmount prizePoolAmount = prizePoolAmountService.getOne(new QueryWrapper<PrizePoolAmount>()
                .eq("flag", "1"));
        if (prizePoolAmount != null) {
            //除以1000
            prizePoolAmount.setAmount((amount.divide(new BigDecimal(1000)).add(prizePoolAmount.getAmount())).setScale(2, BigDecimal.ROUND_HALF_DOWN));
            prizePoolAmountService.updateById(prizePoolAmount);
        }

    }


    /**
     * 百万奖池中房间 排行帮
     * @return
     */
    public R prizePoolRank(){
        //奖池总金额
        BigDecimal amount = new BigDecimal(0);
        String base_amount = propertyService.getV("base_amount");//百万奖池基础金额(心钻)
        List<PrizePoolEntity> prizePoolEntityList = new ArrayList<>();
        List<PrizePoolEntity> prizePoolEntityListTemp = new ArrayList<>();
        PrizePoolAmount prizePoolAmount = prizePoolAmountService.getOne(new QueryWrapper<PrizePoolAmount>()
                .eq("flag", "1"));
        if (prizePoolAmount != null) {
            Date createTime = prizePoolAmount.getCreateTime();
            Map<String, Object> params = new HashMap<>();
            SimpleDateFormat  sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time = sdf.format(createTime);
            params.put("createTime", time);
//            prizePoolEntityList = walletRecordService.prizePoolRank(params);

            prizePoolEntityList = lotteryRecordService.prizePoolRanks(params);
            amount = prizePoolAmount.getAmount().add(new BigDecimal(base_amount));

            QueryWrapper<RankProportion> queryWrapper = new QueryWrapper<>();
            List<RankProportion> rankProportionList = baseMapper.selectList(queryWrapper);
            Map<Integer, Integer> mapProportion = new HashMap<>();
            if (rankProportionList != null && rankProportionList.size() > 0) {
                for (RankProportion rankProportion : rankProportionList) {
                    mapProportion.put(rankProportion.getRanks(), rankProportion.getProportion());
                }
            }

            if (prizePoolEntityList != null && prizePoolEntityList.size() > 0) {
                for(int i = 1; i < prizePoolEntityList.size()+1; i++) {
                    PrizePoolEntity prizePoolEntity = prizePoolEntityList.get(i-1);
                    if (null != mapProportion.get(i)) {
                        prizePoolEntity.setProportion(mapProportion.get(i));
                        //奖池心钻乘以占比
                        prizePoolEntity.setAmount(amount.multiply(new BigDecimal(prizePoolEntity.getProportion())).divide(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_DOWN));
                        prizePoolEntityListTemp.add(prizePoolEntity);
                    }
                }
            }
        }
        int intValue = amount.intValue();//转为int类型
        Map<String, Object> map = new HashMap<>();
        map.put("prizePoolAmount", intValue);
        map.put("list", prizePoolEntityListTemp);
        return R.ok().put("data", map);
    }

}
